package com.example.module.system.controller.admin.captcha;

import cloud.tianai.captcha.application.ImageCaptchaApplication;
import cloud.tianai.captcha.application.vo.CaptchaResponse;
import cloud.tianai.captcha.application.vo.ImageCaptchaVO;
import cloud.tianai.captcha.common.constant.CaptchaTypeConstant;
import com.example.framework.common.pojo.R;
import com.example.module.system.controller.admin.captcha.vo.CaptchaVo;
import com.example.module.system.controller.admin.captcha.vo.TianaiCaptchaVO;
import com.example.module.system.service.captcha.CaptchaService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import org.springframework.web.bind.annotation.*;

@PermitAll
@Tag(name = "防刷验证码", description = "滑块、拼图等类型的验证码")
@RestController
@RequestMapping("/captcha")
public class CaptchaController {

    @Resource
    private CaptchaService captchaService;

    @Resource
    private ImageCaptchaApplication imageCaptchaApplication;

    @Operation(summary = "生成拼图验证码（自实现）")
    @PostMapping("/image/create")
    public R<CaptchaVo> getImageCaptcha(@RequestBody CaptchaVo captcha) {
        CaptchaVo resp = captchaService.getImageCaptcha(captcha);
        return R.success(resp);
    }

    @Operation(summary = "校验拼图验证码（自实现）")
    @GetMapping("/image/check")
    public R<Boolean> checkImageCaptcha(@Parameter(description = "图片的nonceStr") @NotEmpty @RequestParam String uuidStr,
                                        @Parameter(description = "移动距离") @NotNull @RequestParam Integer code) {
        boolean result = captchaService.checkImageCaptcha(uuidStr, code);
        return R.success(result);
    }

    @Operation(summary = "获取数字验证码（自实现）")
    @GetMapping("/number/create")
    public void getNumberCaptcha(HttpServletResponse response) {
        captchaService.getNumberCaptcha(response);
    }

    @Operation(summary = "校验数字验证码（自实现）")
    @GetMapping("/number/check")
    public R<Boolean> checkNumberCaptcha(@Parameter(description = "验证码的nonceStr") @NotEmpty @RequestParam String uuidStr,
                                         @Parameter(description = "验证码") @NotEmpty @RequestParam String code) {
        boolean result = captchaService.checkNumberCaptcha(uuidStr, code);
        return R.success(result);
    }

    @GetMapping("/create")
    @Operation(summary = "tianai-captcha生成验证码")
    public R<CaptchaResponse<ImageCaptchaVO>> createCaptcha(@RequestParam(value = "type", required = false, defaultValue = CaptchaTypeConstant.SLIDER) String type) {
        CaptchaResponse<ImageCaptchaVO> captchaResponse = captchaService.createCaptcha(type);
        return R.success(captchaResponse);
    }

    @PostMapping("/check")
    @Operation(summary = "tianai-captcha校验验证码")
    public R<String> checkCaptcha(@Valid @RequestBody TianaiCaptchaVO captchaVo) {
        String ticket = captchaService.checkCaptcha(captchaVo);
        return R.success(ticket);
    }
}
